//
//  TTVerificationCodeView.m
//  TTBaseProject
//
//  Created by Apple on 16/9/13.
//  Copyright © 2016年 tt. All rights reserved.
//

#import "TTVerificationCodeView.h"

@implementation TTVerificationCodeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
 */

- (void)drawRect:(CGRect)rect {
    //移除view的已有视图控件
    for (UIView *view in [self subviews]) {
        [view removeFromSuperview];
    }
    
    //生成背景色
    float red   = arc4random() % 100 / 100.0;
    float green = arc4random() % 100 / 100.0;
    float blue  = arc4random() % 100 / 100.0;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2];
    [self setBackgroundColor:color];
    
    //@
    //生成文字
    const int count = 4;
    char data[count];
    for (int x = 0; x < count; x++) {
        int j = '0' + (arc4random_uniform(75));
        if ((j >= 58 && j <= 64) || (j >= 91 && j <= 96)) {
            --x;
        } else {
            data[x] = (char)j;
        }
    }
    NSString *textCode = [[NSString alloc] initWithBytes:data length:count encoding:NSUTF8StringEncoding];
    //验证码结果回调
    self.getCode(textCode);
    
    //@
    //设置字体
    CGSize cSize = [@"A" sizeWithAttributes:@{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:30.0]}];
    int width  = self.frame.size.width / textCode.length - cSize.width;
    int height = self.frame.size.height - cSize.height;
    float pX, pY, eX, eY;
    for (int i = 0, count = (int)textCode.length; i < count; i++) {
        pX = arc4random() % width + self.frame.size.width / textCode.length * i - 1;
        pY = arc4random() % height;
        unichar c = [textCode characterAtIndex:i];
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(pX, pY - 25, 120,82)];
        tempLabel.backgroundColor = [UIColor clearColor];
        
        //字体颜色
        UIColor *codeColor = [UIColor colorWithRed:arc4random() % 100 / 100.0 green:arc4random() % 100 / 100.0 blue:arc4random() % 100 / 100.0 alpha:1.0];
        
        NSString *textC = [NSString stringWithFormat:@"%C",c];
        tempLabel.textColor = codeColor;
        tempLabel.text = textC;
        tempLabel.font = [UIFont systemFontOfSize:(14+(arc4random() % (30 -14+1)))];
        [self addSubview:tempLabel];
    }
    
    //@
    //干扰线
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    for (int i = 0; i < count; i++) {
        color = [UIColor colorWithRed:arc4random() % 100 / 100.0 green:arc4random() % 100 / 100.0 blue:arc4random() % 100 / 100.0 alpha:1.0];
        CGContextSetStrokeColorWithColor(context, [color CGColor]);
        pX = arc4random() % (int)self.frame.size.width;
        pY = arc4random() % (int)self.frame.size.height;
        CGContextMoveToPoint(context, pX, pY);
        eX = arc4random() % (int)self.frame.size.width;
        eY = arc4random() % (int)self.frame.size.height;
        CGContextAddLineToPoint(context, eX, eY);
        CGContextStrokePath(context);
    }
}


@end


#pragma mark - 其他信息
//   干扰线
/* UIGraphicsGetCurrentContext()  获取上下文的方法无用
 
   此方法在draw中才可以，此处无法获取，为nil
 
   考虑创建一个对象，每次初始化的时候简练各个参数（颜色、随机值等），创建后回调检校码
 */

//    获取随机数
//    1、  获取一个随机整数范围在：[0,100)包括0，不包括100
//         int x = arc4random() % 100;
//    2、  获取一个随机数范围在：[500,1000），包括500，包括1000
//         int y = (arc4random() % 501) + 500;
//    3、  获取一个随机整数，范围在[from,to），包括from，包括to
//         - (int)getRandomNumber:(int)from to:(int)to
//    {
//        return (int)(from + (arc4random() % (to – from + 1)));
//    }

//    绘制干扰线
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetLineCap(context, kCGLineCapRound);
//    CGContextSetLineWidth(context, 3);  //线宽
//    CGContextSetAllowsAntialiasing(context, true);
//    CGContextSetRGBStrokeColor(context, 70.0 / 255.0, 241.0 / 255.0, 241.0 / 255.0, 1.0);  //线的颜色
//    CGContextBeginPath(context);
//    CGContextMoveToPoint(context, 0, 0);  //起点坐标
//    CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);   //终点坐标
//    CGContextStrokePath(context);
